# 将做好的数据 进行识别
# 需要调用学习网络
import numpy
import matplotlib
from 神经网络 import neuralNetwork

# 初始化数据
input_nodes = 784
hidden_nodes = 200
output_nodes = 10
learning_rate = 0.2
#调用神经网络
n = neuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)

# 训练载入
training_data_file = open('C:\\Users\\kai\\Desktop\\邮科院学习\\神经网络数据\\mnist_train_100.csv','r')
training_data_list = training_data_file.readlines()
training_data_file.close()
# 开始实际训练
epochs = 10
for e in range(epochs):
    for record in training_data_list:
        all_values = record.split(',')
        inputs = (numpy.asfarray(all_values[1:])/255.0*0.99)+0.01
        targets = numpy.zeros(output_nodes)+0.01
        targets[int(all_values[0])]=0.99
        n.train(inputs, targets)
        pass
    pass

# 载入测试部分
test_data_file = open('C:\\Users\\kai\\Desktop\\邮科院学习\\神经网络数据\\my_diy0.csv','r')
test_data_list = test_data_file.readlines()
test_data_file.close()


all_values = test_data_list[1].split(',')
# 正确的数字
correct_label = all_values[1]
# 整理格式化输入列表
inputs = numpy.asfarray(all_values[1:])
# 获取输出结果
outputs = n.query(inputs)
# 从输出结果中获取标记出来的数字
label = numpy.argmax(outputs)
print(outputs)
print("图像中的数字是：", label)

# 将矩阵转化为图像
image_array = numpy.asfarray(all_values[1:]).reshape(28, 28)
matplotlib.pyplot.imshow(image_array, cmap='Greys', interpolation='None')
matplotlib.pyplot.show()